<?php 
    require 'common.php';
    $csvFile = 'db.csv';
    $csvFile2 = 'user_decrypt.csv';
    
     $mappingArray = array(
         'No.' => 'no',
         'SV' => 'SVID',
         '代理店' => 'AgencyID',
         '営業' => 'BusinessCode',
         '担当者' => 'BusinessName',
         '氏名' => 'Name1',
         'Column1' => 'Name2',
         'せい' => 'Kana1',
         'めい' => 'Kana2',
         'メールアドレス' => 'Mail',
         'S.STATION受理日' => 'RegDate',
         'S.STATION会員ID' => 'ID',
         'S.STATION会員ID2' => 'NewID',
         'First' => 'EngName1',
         'Name' => 'EngName2',
         '郵便番号' => 'Zip',
         '住所１' => 'Address1',
         '住所２' => 'Address2',
         '連絡先' => 'Tel',
         '生年月日' => 'BirthDate',
         '年齢' => 'Age',
         'S.STATION入金先' => 'bank_name',
         '決済番号' => 'payment_code',
         'S.STATION入金日' => 'payment_date',
         'S.STATION発送日' => 'invoice_date',
         '次回更新月' => 'next_update',
         '対象キャンペーン' => 'campaign',
         '旧更新月' => 'last_update',
         '更新入金先' => 'payment_address_new',
         '決済番号2' => 'payment_code_new',
         '更新入金日' => 'payment_date_new',
         '更新Thanksメール日' => 'thank_mail_date_new',
         '全額出金者' => 'depositor',
         '海外ツアー／口座開設' => 'oversea_tour_open_account',
         'その他' => 'other'
     );
    $csv = csv2arr($csvFile, $mappingArray);
    
    $bankArr = array(
    	'bank_name' => '1',
        'payment_code' => '1',
        'payment_date' => '1',
        'invoice_date' => '1',
        'next_update' => '1',
        'campaign' => '1',
        'last_update' => '1',
        'payment_address_new' => '1',
        'payment_code_new' => '1',
        'payment_date_new' => '1',
        'thank_mail_date_new' => '1',
        'depositor' => '1',
        'oversea_tour_open_account' => '1',
        'other' => '1'
    );
    
    $userArr = array(
        'ID' => '1',
    	'Name1' => '1',
        'Name2' => '1',
        'Mail' => '1',
        'Pass' => 'default',
    );
    
    $userDetailArr = array(
    	'SVID' => '1',
        'AgencyID' => '1',
        'BusinessCode' => '1',
        'BusinessName' => '1',
        'Kana1' => '1',
        'Kana2' => '1',
        'EngName1' => '1',
        'EngName2' => '1',
        'Address1' => '1',
        'Address2' => '1',
        'Tel' => '1',
        'BirthDate' => '1',
        'Zip' => '1'
    );
    foreach ($userDetailArr as $k => $v) {
        $userDetailArr[$k] = $k;
    }
    
    $csvData2 = csv2arr($csvFile2);
    $mappingArr = array();
    foreach ($csvData2 as $key => $row) {
        if ($row['ID'] != '') {
            $mappingArr[$row['ID']] = $row['Pass'];
        }
    }
    
    /* debug(count($bankArr) + count($userArr) + count($userDetailArr));
    debug(count($mappingArray)); */
    $results = array();
    foreach ($csv as $row) {
        $resultRow = array(
            'user' => $userArr,
            'userdetail' => $userDetailArr,
            'useroldbank' => $bankArr
        );
        foreach ($row as $k => $v) {
            if (!empty($userArr[$k])) {
                $resultRow['user']["$k"] = $v;
            } elseif (!empty($userDetailArr[$k])) {
                $resultRow['userdetail']["$k"] = $v;
            } elseif (!empty($bankArr[$k])) {
                $resultRow['useroldbank']["$k"] = $v;
            }
        }
        
        if ($mappingArr[$resultRow['user']['ID']]) {
            $resultRow['user']['Pass'] = $mappingArr[$resultRow['user']['ID']]; 
        }
        
        $results[] = $resultRow;
//         debug($resultRow);
//         die();
    }
    debug($results);
    
    
    